<template>
  <a-modal
    title="编辑菜单权限"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
    :destroyOnClose="true"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="唯一识别码">
          <a-input
            placeholder="唯一识别码"
            v-decorator="['code', { rules: [{ required: true, message: '唯一识别码不能为空！' }] }]"
          />
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="权限名称">
          <a-input
            placeholder="权限名称"
            v-decorator="['name', { rules: [{ required: true, message: '权限名称不能为空！' }] }]"
          />
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="状态">
          <a-select
            placeholder="状态"
            v-decorator="['status', { rules: [{ required: true, message: '状态不能为空！' }] }]"
          >
            <a-select-option value="1">正常</a-select-option>
            <a-select-option value="2">禁用</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="描述" hasFeedback>
          <a-textarea :rows="3" placeholder="描述" v-decorator="['description']" />
        </a-form-item>
        <a-divider />

        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="可操作权限" hasFeedback>
          <a-select
            style="width: 100%"
            mode="multiple"
            :allowClear="true"
            placeholder="可操作权限"
            v-decorator="['actions', { rules: [{ required: true, message: '赋予权限不能为空！' }] }]"
          >
            <a-select-option v-for="(action, index) in permissionList" :key="index" :value="action.value">{{
              action.label
            }}</a-select-option>
          </a-select>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
// import moment from 'moment'
import { editPermission } from '@/api/role'
export default {
  name: 'EditModal',
  data () {
    return {
      visible: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      mdl: {},
      update_time: '2020-04-28 07:02:00',
      // 向后端拉取可以用的操作列表
      permissionList: [
        { label: '新增', value: 'add', defaultChecked: false },
        { label: '查询', value: 'query', defaultChecked: false },
        { label: '详情', value: 'details', defaultChecked: false },
        { label: '修改', value: 'update', defaultChecked: false },
        { label: '列表', value: 'get', defaultChecked: false },
        { label: '删除', value: 'delete', defaultChecked: false }
        // { label: '导入', value: 'import', defaultChecked: false },
        // { label: '导出', value: 'export', defaultChecked: false }
      ],
      form: this.$form.createForm(this)
    }
  },

  methods: {
    // 选中创建时间
    createdChange (value, dateString) {
      console.log('Formatted Selected Time: ', dateString)
    },
    // 选中创建时间
    updateChange (value, dateString) {
      console.log('Formatted Selected Time: ', dateString)
    },
    compareToFirstPassword (rule, value, callback) {
      const form = this.form
      if (value && value !== form.getFieldValue('password')) {
        // callback('Two passwords that you enter is inconsistent!')
        callback(new Error('两次输入密码不一致'))
      } else {
        callback()
      }
    },
    validateToNextPassword (rule, value, callback) {
      const form = this.form
      if (value && this.confirmDirty) {
        form.validateFields(['confirm'], { force: true })
      }
      callback()
    },
    edit (record) {
      this.mdl = Object.assign({}, record)
      console.log('接收列表行参数-----', this.mdl)
      this.visible = true
      const {
        form: { setFieldsValue }
      } = this
      this.$nextTick(() => {
        setFieldsValue({
          id: this.mdl.id,
          code: this.mdl.code,
          name: this.mdl.name,
          status: this.mdl.status,
          actions: this.mdl.actions,
          description: this.mdl.description
          // created_time: moment(this.mdl.created_time),
          // created_by: this.mdl.created_by,
          // update_time: moment(this.mdl.update_time),
          // update_by: this.mdl.update_by
        })
      })
    },

    handleSubmit () {
      const {
        form: { validateFields }
      } = this
      this.confirmLoading = true

      validateFields((errors, values) => {
        console.log('errors:', errors, 'val:', values)
        values.id = this.mdl.id
        if (!errors) {
          values.actions = JSON.stringify(values.actions)
          editPermission(values).then((res) => {
            if (res) {
              // 存储成功、刷新列表
              this.$emit('ok', values)
              this.visible = false
              this.confirmLoading = false
              this.$notification['success']({
                message: '提示',
                description: '数据修改成功！'
              })
            }
          })
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel () {
      this.visible = false
      this.confirmLoading = false
    }
  }
}
</script>
